/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
**/
define("pages/wlrydj/wlrydj_form", [
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        '../../pages/wlrydj/wlrydj_form.html.js',
        "../../common/componts",
        "../../common/tools"
    ],
    function(require){
        var layerIndex;
        var _callback;
        var formTemplate = require("./wlrydj_form.html");
        /**
         * 获取表单内容
         * @param initData
         * @returns {*}
         */
        var getFormHtml = function(initData){
            if (!initData) {
                initData = {};
                initData.rybh='';
                initData.jsbh='';
            }
            var htmlCompile = template.compile(formTemplate);
            var username = CURRENT_USER.username;//获取当前办理人
            var html = htmlCompile({
                tbr:username,
                tbrq:currentDate(),
                data:initData
            });
            return html;
        };
        /**
         * 动态渲染
         */
        var parseEasyUIElement = function(){
            $.parser.parse($(".awd-form-panel"));
        };

        var _show = function(initData,callback){
            _callback = callback;
            var componts = require("../../common/componts");

            layerIndex = layer.open({
                type: 1,
                title: "数据录入",
                skin: 'layui-layer-lan', //加上边框
                closeBtn: 1, //不显示关闭按钮
                shadeClose: true, //开启遮罩关闭
                content: getFormHtml(initData),
                area:['100%','100%'],
                success: function(layero, index){
                    console.log(layero, index);
                }
            });
            layer.full(layerIndex);
            setTimeout(function(){
                //初始化下拉框
                componts.initSelectPage(undefined);
                componts.initSelect(undefined,undefined,undefined);
                parseEasyUIElement(); //动态渲染
            },100);
        };

        var save = function(){
            var jbxxcount=$(".cjyryxx-form").find("tr").length-1;
            var jbxxarr=[];
            for(var i=1;i<=jbxxcount;i++){
                var xm = $("#xm"+i).val();
                var zjh = $("#zjh"+i).val();
                var dw = $("#dw"+i).val();
                var dh = $("#dh"+i).val();
                var snzjh = $("#snzjh"+i).val();
                var jbxx = {"xm":xm,"zjh":zjh,"dw":dw,"dh":dh,"snzjh":snzjh};
                jbxxarr.push(jbxx);
            }
            console.log(jbxxarr);
            var jbxxjson = JSON.stringify(jbxxarr);

            var clxxcount=$(".clxx-form").find("tr").length-1;
            var clxxarr=[];
            for(var i=1;i<=clxxcount;i++){
                var clssdw = $("#clssdw"+i).val();
                var cphm = $("#cphm"+i).val();
                var aqjcqk = $("#aqjcqk"+i).val();
                var snztzhm = $("#snztzhm"+i).val();
                var clxx = {"clssdw":clssdw,"cphm":cphm,"aqjcqk":aqjcqk,"snztzhm":snztzhm};
                clxxarr.push(clxx);
            }
            console.log(clxxarr);
            var clxxjson = JSON.stringify(clxxarr);

            var url = "/wlrydj/save";
            var $form = $(".awd-form");
            if (!$form.form('validate')) {
                $.messager.alert("提示", '必填项不能为空！');
                return;
            }
            var ywdata = $form.serializeObject();
            var _data = JSON.stringify(ywdata);
            // console.log(clxxarr);

            var data = {jsonList:_data,dataRyxx:jbxxjson,dataClxx:clxxjson};
            // var jbxxjson = JSON.stringify($(".cjyryxx-form").serializeObject());
            // var clxxjson = JSON.stringify($(".clxx-form").serializeObject());
            // var params = $.extend(true,ywdata,jbxxjson,clxxjson);
            // console.log(params);
            var data = {jsonList:_data,dataRyxx:jbxxjson,dataClxx:clxxjson};
            console.log(data);
            var loadingIndex = layer.load();
            $.ajax({
                type : "POST",  //提交方式
                url : url,
                data : data,//数据，这里使用的是Json格式进行传输
                success : function(result) {
                    // layer.closeAll();
                    if (result.success) {
                        _callback && _callback();
                        $.messager.alert("确定","保存成功","info");
                    }else{
                        $.messager.alert("确定","保存失败","warn");
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        };

        var bindAction = function(){
            //保存按钮事件
            $(document).on('click','.save-button',function () {
                save();
            });
            //点击关闭按钮
            $(document).on('click','.close-button',function(){
                layer.close(layerIndex);
                layerIndex = null;
            });

            var serialNum = 4;
            var cjryxxHeight= parseInt(41);
            $(document).on('click','.cjryxx .add-row',function(){
                var jbxxcount=$(".cjyryxx-form").find("tr").length-1;
                console.log(jbxxcount+"+++++++++++");
                serialNum++;
                var trId = 'record_' + serialNum;
                var trHtml="<tr id='"+trId+"' class='"+trId+"'>"+
                    "<td><input id='xm"+(jbxxcount+1)+"'name='xm' type='text' class='form-control'/></td>"+
                    "<td><input id='zjh"+(jbxxcount+1)+"'name='zjh' type='text' class='form-control'/></td>"+
                    "<td><input id='dw"+(jbxxcount+1)+"'name='dw' type='text' class='form-control'/></td>"+
                    "<td><input id='dh"+(jbxxcount+1)+"'name='dh' type='text' class='form-control'/></td>"+
                    "<td><input id='snzjh"+(jbxxcount+1)+"'name='snzjh' type='text' class='form-control'/></td>"+
                    "<td><button>扫描</button></td>"+
                    "<td><a href='javascript:void(0)' class='add-row' data-id='"+trId+"' title='增加'>+</a>&nbsp;<a href='javascript:void(0)' class='del-row' data-id='"+trId+"' title='删除'>-</a></td>"+
                    "</tr>";
                $(".cjryxx").find("tr:last").parent().append(trHtml);
                // $('.cjryxx').attr('style','height:'+($('.cjryxx').height()+cjryxxHeight)+'px;')
            });

            $(document).on('click','.cjryxx .del-row',function(){
                var trId = $(this).data("id");
                $("#"+trId).remove();
                // $('.cjryxx').attr('style','height:'+($('.cjryxx').height()-cjryxxHeight)+'px;')
            });

            //车辆信息
            $(document).on('click','.clxx .add-car-row',function(){
                var clxxcount=$(".clxx-form").find("tr").length-1;
                console.log(clxxcount+"=============");
                serialNum++;
                var trId = 'record_car_' + serialNum;
                var trHtml="<tr id='"+trId+"' class='"+trId+"'>"+
                    "<td><input id='clssdw"+(clxxcount+1)+"' name='clssdw' type='text' class='form-control'/></td>"+
                    "<td><input id='cphm"+(clxxcount+1)+"' name='cphm' type='text' class='form-control'/></td>"+
                    "<td><input id='aqjcqk"+(clxxcount+1)+"' name='aqjcqk' type='text' class='form-control'/></td>"+
                    "<td><input id='snztzhm"+(clxxcount+1)+"' name='snztzhm' type='text' class='form-control'/></td>"+
                    "<td><a href='javascript:void(0)' class='add-car-row' data-id='"+trId+"' title='增加'>+</a>&nbsp;<a href='javascript:void(0)' class='del-car-row'  data-id='"+trId+"' title='删除'>-</a></td>"+
                    "</tr>";
                $(".clxx").find("tr:last").parent().append(trHtml);
                // $('.clxx').attr('style','height:'+($('.clxx').height()+cjryxxHeight)+'px;')
            });

            $(document).on('click','.clxx .del-car-row',function(){
                var trId = $(this).data("id");
                $("#"+trId).remove();
                // $('.clxx').attr('style','height:'+($('.clxx').height()-cjryxxHeight)+'px;')
            });

            //修正layer 最大化，解决办法..
            $(window).resize(function(){
                layerIndex && layer.full(layerIndex);
                parseEasyUIElement();//TODO::后续优化
            })
        };

        (function(){
            var componts = require("../../common/componts");
            componts.initSelectPage(undefined);
            componts.initSelect(undefined,undefined,undefined);
            bindAction();
        })()

        return{
            show:_show
        };
    });